﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using RPSImporter.Infrastructure;
using RPSImporter.Infrastructure.Attributes;
using RPSImporter.Services.Data;
using RPSImporter.Models.Settings;
using RPSImporter.Services;

namespace RPSImporter.Models
{
   [FileTypeName("Hierarchy_FileType")]
   public class Hierarchy : ModelBase, IModel
   {
      public Hierarchy()
      {
         this.FileSettings = DatabaseSvc.Instance.Settings.HierarchyFileSettings.First();
      }

      public HierarchyFileSettings FileSettings { get; set; }

      public bool Import()
      {
         try
         {
            if (this.GetData(this.FileSettings.Code) != String.Empty
               && !this.DB.HIERARCHies.Any(h => h.Code == this.GetData(this.FileSettings.Code)))
            {
               HIERARCHY hierarchy = new HIERARCHY();
               hierarchy.Code = this.GetData(this.FileSettings.Code);
               hierarchy.NAME = this.GetData(this.FileSettings.Name);

               if (DatabaseSvc.Instance.Settings.GeneralSettings.FirstOrDefault().IsMultiColumnPersonImport)
               { 
                  //TODO: multi column import like in Person.cs
                  var hierarchyID = (from o in this.DB.HIERARCHies
                                     let firstID = this.DB.HIERARCHies.FirstOrDefault(h => h.COMPANY.NAME == this.GetData(this.FileSettings.Company) && h.type == 1).HIERARCHY_ID
                                     let secondID = this.DB.HIERARCHies.FirstOrDefault(h => h.COMPANY.NAME == this.GetData(this.FileSettings.Enterprise) && h.type == 2 && h.PARENT_ID == firstID).HIERARCHY_ID
                                     let thirdID = this.DB.HIERARCHies.FirstOrDefault(h => h.COMPANY.NAME == this.GetData(this.FileSettings.Department) && h.type == 3 && h.PARENT_ID == secondID).HIERARCHY_ID
                                     let forthID = this.DB.HIERARCHies.FirstOrDefault(h => h.COMPANY.NAME == this.GetData(this.FileSettings.Sector) && o.type == 3 && h.PARENT_ID == thirdID).HIERARCHY_ID
                                     where o.HIERARCHY_ID == forthID
                                     select o).FirstOrDefault().HIERARCHY_ID;
                  hierarchy.HIERARCHY_ID = hierarchyID;
               }
               else
               {
                  hierarchy.PARENT_ID = this.GetData(this.FileSettings.CodeParent) != String.Empty ?
                                               long.Parse(this.GetData(this.FileSettings.CodeParent)) : (long?)null;
               }
               this.DB.HIERARCHies.InsertOnSubmit(hierarchy);
               this.DB.SubmitChanges();
            }
            else
            {
               HIERARCHY hierarchy = this.DB.HIERARCHies.FirstOrDefault(h => h.Code == this.GetData(this.FileSettings.Code));
               hierarchy.Code = this.GetData(this.FileSettings.Code);
               hierarchy.NAME = this.GetData(this.FileSettings.Name);
               hierarchy.PARENT_ID = this.GetData(this.FileSettings.CodeParent) != String.Empty ?
                                            long.Parse(this.GetData(this.FileSettings.CodeParent)) : (long?)null;
               this.DB.SubmitChanges();
            }
            return true;
         }
         catch (Exception ex)
         {
            this.Error = ex.Message;
            return false;
         }
      }
   }
}